package com.baomidou.plugin.idea.mybatisx.advance.processor.clazz;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.plugin.idea.mybatisx.advance.util.PsiClassUtil;
import com.baomidou.plugin.idea.mybatisx.advance.util.PsiMethodUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 给po的每个属性都生成一个方法比如username 生成username()
 * @author wanglei
 */
public class FieldMethodProcessor extends AbstractMPOnceMethodGenClassProcessor {

    private static final String CLASS_NAME = TableName.class.getName();

    public static final String METHOD_NAME = "fieldMethod";

    public FieldMethodProcessor() {
        super(CLASS_NAME, PsiMethod.class);
    }

    @Override
    String getMethodName() {
        return METHOD_NAME;
    }

    @NotNull
    public Collection<PsiMethod> createToStringMethod(@NotNull PsiClass psiClass, @NotNull PsiElement psiNavTargetElement) {
        final PsiField[] fields = PsiClassUtil.collectClassFieldsIntern(psiClass);
        List<PsiMethod> result = new ArrayList<>();
        try {
            for (PsiField field : fields) {
                final StringBuilder builder = new StringBuilder();
                builder.append("public " + psiClass.getName() + " " + field.getName() + "(){return this;} ");
                result.add(PsiMethodUtil.createMethod(psiClass, builder.toString(), psiNavTargetElement));
            }
        } finally {
            //StringBuilderSpinAllocator.dispose(builder);
        }
        return result;
    }

}
